<%@ page contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<script type="text/javascript">
$(document).ready(function() { 
	$('#tree_sitetype').tree({
		url: '<c:url value="/system/site/sitetype/findSiteTypeTree.do"/>',
		onBeforeExpand: function(node) {
			var url = '<c:url value="/system/site/sitetype/findSiteTypeTree.do"/>?parentcode='+ node.id;
			$('#tree_sitetype').tree('options').url = url;
		},
		onLoadSuccess: function(node, data) {
			if($('#tree_sitetype').tree('getRoot')!=null){
				$('#tree_sitetype').tree('expand', $('#tree_sitetype').tree('getRoot').target);
			}
		},
		onSelect: function(node) {
			sitetypereloadgrid(node.id);
			if(node.id!=null&&node.id!='')
				$('#sitetypeCenter').panel('setTitle','网站分类管理-'+node.text);
			else
				$('#sitetypeCenter').panel('setTitle','网站分类管理');
		},
		onDblClick: function(node) {
        	/*展开或折叠树节点*/
			$(this).tree('toggle', node.target);
        }
	});

	$('#sitetypeList').datagrid({
		fit: true,
		title:'',
		nowrap: false,
		striped: true,
		singleSelect: true,
		url: '<c:url value="/system/site/sitetype/jsonSitetypePageList.do"/>',
		queryParams:{parentcode:'0'},
		sortName: 'typecode',
		sortOrder: 'asc',
		idField: 'id',
		frozenColumns: [[
			{field:'ck',checkbox:true},
			{field:'typecode',title:'分类代码',width:100,sortable:true},
		    {field:'typename',title:'分类名称',width:150,sortable:true}
		]],
		columns: [[
		    {field:'parentcode',title:'上级编码',width:100,sortable:true},
			{field:'keyword',title:'关键词',width:290,sortable:true},
			{field:'typesort',title:'排序字段',width:80,sortable:true},
			{field:'typestate',title:'状态',width:80,sortable:true,formatter:function(value){
				return value=="0"?"禁用":"启用";
			}}
		]],
		toolbar: [{
			text: '新增',
			iconCls: 'icon-add',
			handler: function(){
				addSiteType(1);
			}
		},{
			text: '删除',
			iconCls: 'icon-remove',
			handler: function(){
				delSiteType();
			}
		},{
			id: 'modify',
			text: '修改',
			iconCls: 'icon-edit',
			handler: function(){
				addSiteType(2);
			}
		}],
		pageSize: 20,
		pagination: true,
		rownumbers: true,
		onClickRow:function(rowIndex, rowData) {
			$('#sitetypeList').datagrid('clearSelections').datagrid('selectRow',rowIndex);	
		}
	});
	
});

function sitetypereloadgrid(pid){
	$('#sitetypeList').datagrid('getPager').pagination({pageNumber:1});
	var queryParams = $('#sitetypeList').datagrid('options').queryParams;
	if(typeof(queryParams.parentcode)=="undefined")
		queryParams.parentcode = $('#tree_sitetype').tree('getRoot').id;
	else
		queryParams.parentcode = pid;
	$('#sitetypeList').datagrid('options').queryParams = queryParams;
	$('#sitetypeList').datagrid('clearSelections').datagrid('load'); 
	//$('#sitetypeList').datagrid('options').url = '<c:url value="/system/site/sitetype/jsonSitetypePageList.do"/>?parentcode='+pid;
	//$("#sitetypeList").datagrid('load');
}

function addSiteType(flag){
	var url = '<c:url value="/system/site/sitetype/sitetypeadd.do"/>';
	var parentcode;
	if($('#tree_sitetype').tree('getSelected')!=null){
		parentcode = $('#tree_sitetype').tree('getSelected').id;
	}else{
		parentcode = $('#tree_sitetype').tree('getRoot').id;
	}
	var title;
	if(flag==1){
		title = '网站分类添加';
		url = url +'?pid='+parentcode;
	}else{
		title = '网站分类修改';
		var sitetypeRow = $('#sitetypeList').datagrid('getSelected');
		var st_id = sitetypeRow.id;
		url = url +'?id='+st_id;
	}
	$('#dlgAddSiteType').dialog('close').remove();
	$('body').append('<div id="dlgAddSiteType" class="easyui-dialog" closed="true"></div>');
	$('#dlgAddSiteType').dialog({
		title:title,
		modal:true,
		singleSelect:true,
		closable:true,
		draggable:false,
		width:630,
		height:240,
		toolbar:[{
			text:'保存',
			iconCls:'icon-save',
			handler:function(){
				saveSiteType();
			}
		},{
			text:'关闭',
			iconCls:'icon-cancel',
			handler:function(){
				$('#dlgAddSiteType').dialog('close');
			}
		}]
	}).dialog('open').dialog('refresh',url);
}



function delSiteType(){
	var row = $('#sitetypeList').datagrid('getSelected');
	if(row == null){
		$.messager.alert('错误', '请选择您要删除的数据！', 'error'); 
		return;
	}
	else {
		$.messager.confirm('警告', '您确认删除么?', function(r){
			if(r){
				$.ajax({
					url: '<c:url value="/system/site/sitetype/delSiteType.do"/>',
					type: 'POST',
					data: 'id='+row.id,
					beforeSend: function(XmlHttpRequest, textStatus, errorThrown) {
						$.messager.progress({
							title:'提示',
							msg:'正在执行数据操作，请稍待...'
						});
					},
					success: function(result) {
						$.messager.progress('close');
						if(!result.success) {
							$.messager.alert('错误', result.data , 'error');
						}
						else {
							$.messager.alert('提示', result.data , 'info');		
							var node = $('#tree_sitetype').tree('getSelected');  //组织节点须选中
							if (node == null){
								node = $('#tree_sitetype').tree('getRoot');
							}
							$('#tblListAdmdiv').datagrid('clearSelections').datagrid('reload');
							$('#tree_sitetype').tree('getData',node.target).url = '<c:url value="/system/site/sitetype/findSiteTypeTree.do"/>?parentcode='+node.id;
							$('#tree_sitetype').tree('reload', node.target);
							$('#tree_sitetype').tree('expand', node.target);
							$("#sitetypeList").datagrid('clearSelections').datagrid('reload');
						}
					}
				});
			}
		});
	}
}
</script>

<!-- //页面布局 -->
<div class="easyui-layout" fit="true" style="background:#ccc;">
	<div region="west" split="true" title="网站分类树"style="width:200px;padding:0px;">
			<ul id="tree_sitetype"></ul>
	</div>
	<div id="sitetypeCenter" region="center" title="网站分类管理"> 
		<table id="sitetypeList" border="false" cellpadding="0"  cellspacing="0"> </table>
	</div>
</div>